import { cacheTools } from 'nc-lightapp-front';
import deepCopy from 'src/hrpub/common/utils/deep-copy';
export default class cardAction {
	constructor(comp) {
		this.comp = comp;
	}
	/* 控制审核modal显示 */
	approveOpen = () => {
		const { dispatch } = this.comp.props;
		dispatch({
			type: 'psninfoApprove/update',
			payload: {
				approveModalShow: true,
				approveStatus: 0
			}
		});
	};
	/* 设置详情页数据 */
	setDetailData = (record, index) => {
		const { props } = this.comp;
		const { dispatch, psninfoApprove, cardPagination, form, button, meta } = props;
		const { lang } = psninfoApprove;
		let pkId = record.values !== undefined ? record.values.pk_psninfoapprove.value : record,
			_meta_ = meta.getMeta();
		// 防止闪烁,先把之前的渲染清除,再获取数据后重新渲染
		dispatch({
			type: 'psninfoApprove/update',
			payload: {
				infochangeDetail: {}
			}
		});
		/* 获取数据 */
		dispatch({
			type: 'psninfoApprove/getDetailData',
			payload: {
				postData: {
					pk_org: psninfoApprove.hrorgobj.refpk,
					pk_psninfoapprove: pkId
				}
			}
		}).then((res) => {
			let hasDatas = JSON.stringify(res.data) === '{}' ? false : true;
			// let allpks = cacheTools.get('allpks');
			if (hasDatas) {
				if (res.data.ptArea && res.data.ptArea.hasOwnProperty('areas')) {
					let temp = res.data.ptArea.areas;
					// let tablecodes = []
					for (let itemCode in temp) {
						if (temp[itemCode].moduletype === 'table') {
							this.comp.props.use.editTable(itemCode);
							temp[itemCode].items.splice(0, 0, {
								attrcode: 'changeType',
								itemtype: 'customer',
								hyperlinkflag: false,
								label: lang['gx6008-000262'], // 国际化 '变更类型'
								width: '100px',
								textAlign: 'center',
								visible: true,
								render: (text, record, index) => {
									let __text__ = '';
									switch (record.status) {
										case '1':
											__text__ = lang['gx6008-000038'];
											break;
										case '2':
											__text__ = lang['gx6008-000049'];
											break;
										case '33':
											__text__ = lang['gx6008-000039'];
											break;
										default:
											__text__ = lang['gx6008-000202'];
									}
									return <span>{__text__}</span>;
								}
							});
						}
						temp[itemCode].items.forEach((item) => {
							if (item.itemtype === 'residtxt') item.itemtype = 'input';
							item['attrcode'] = item.code;
							delete item.code;
						});
					}
					Object.assign(_meta_, temp);
					
				}
				
				meta.setMeta(_meta_,async ()=>{
					await dispatch({
						type: 'psninfoApprove/update',
						payload: {
							infochangeDetail: res.data
						}
					})
					this.getChangeDetail()
				
				})
				// if (res.data.headform['psninfoappform'].rows[0].values.checkstatus.value !== '0') {
				// 	button.setButtonDisabled([ 'approve' ], true);
				// }
				form.setAllFormValue({ psninfoappform: { rows: res.data.headform['psninfoappform'].rows } });
				// cardPagination.setCardPaginationId({ id: allpks[index], status: 1 });
				
			}
		});
	};
	/* 设置变更信息列表 */
	getChangeDetail = () => {
		const { props } = this.comp;
		const { psninfoApprove, editTable, form, meta } = props;
		const { infochangeDetail } = psninfoApprove;
		if (infochangeDetail.hasOwnProperty('gridMap') && JSON.stringify(infochangeDetail.gridMap) !== '{}') {
			let rowDatas = [];
			if (infochangeDetail.gridMap.hasOwnProperty('newGrid')) {
				infochangeDetail.gridMap['newGrid'][infochangeDetail.infoset_code].rows.forEach((item, index) => {
					if (item.status === '3') {
						item.status = '33';
					}
					rowDatas.push(item);
					if (
						infochangeDetail.gridMap.hasOwnProperty('oldGrid') &&
						infochangeDetail.gridMap['oldGrid'][infochangeDetail.infoset_code].rows[index]
					) {
						rowDatas.push(infochangeDetail.gridMap['oldGrid'][infochangeDetail.infoset_code].rows[index]);
					}
				});
			}
			editTable.setTableData(infochangeDetail.infoset_code, { rows: rowDatas });
		}
		if (infochangeDetail.hasOwnProperty('formMap') && JSON.stringify(infochangeDetail.formMap) !== '{}') {
			let _meta_ = meta.getMeta();
			let hiddenItem = {};
			// 图片特殊处理
			_meta_[infochangeDetail.infoset_code].items.forEach((item) => {
				// item.disabled = false;
				// item.visible = true;
				if (item.attrcode === 'photo') {
					item.render = (value, record, index) => {
						let _src_ = record['photo'].value ? `data:image/jpg;base64,${record['photo'].value}` : null;
						return _src_ ? (
							<img
								style={{
									maxHeight: '50px'
								}}
								src={_src_}
							/>
						) : (
							_src_
						);
					};
				}
			});
			// meta.setMeta(_meta_);
			// 变更后 form
			let newcode = 'new_'
			let oldcode = 'old_'
			if (infochangeDetail.formMap.hasOwnProperty('newForm')) {
				let newMeta = deepCopy(_meta_[infochangeDetail.infoset_code]),
					metaObj = {};
				newMeta.code = 'new_' + infochangeDetail.infoset_code;
				newcode = newMeta.code
				metaObj[newMeta.code] = newMeta;
				Object.assign(_meta_, metaObj);

				infochangeDetail.formMap['newForm'][infochangeDetail.infoset_code].rows.forEach((item, index) => {
					for (let key in item.values) {
						if (JSON.stringify(item.values[key]) === '{}') {
							hiddenItem[key] = false;
						}else{
							hiddenItem[key] = true;
						}
					}
				});
				// debugger;
				
			}
			// 变更前 form
			if (infochangeDetail.formMap.hasOwnProperty('oldForm')) {
				let oldMeta = deepCopy(_meta_[infochangeDetail.infoset_code]),
					metaObj = {};
				oldMeta.code = 'old_' + infochangeDetail.infoset_code;
				oldcode = oldMeta.code
				metaObj[oldMeta.code] = oldMeta;
				Object.assign(_meta_, metaObj);
				infochangeDetail.formMap['oldForm'][infochangeDetail.infoset_code].rows.forEach((item, index) => {
					for (let key in item.values) {
						if (JSON.stringify(item.values[key]) !== '{}') {
							hiddenItem[key] = true;
						}
					}
				});
				
			}
			meta.setMeta(_meta_,(s)=>{
				if(infochangeDetail.formMap.hasOwnProperty('newForm')){
					form.setFormItemsVisible(newcode, hiddenItem);
					form.setAllFormValue({
						[newcode]: infochangeDetail.formMap['newForm'][infochangeDetail.infoset_code]
					});
				}
				if(infochangeDetail.formMap.hasOwnProperty('oldForm')){
					form.setFormItemsVisible(oldcode, hiddenItem);
					form.setAllFormValue({
						[oldcode]: infochangeDetail.formMap['oldForm'][infochangeDetail.infoset_code]
					});
				}
			});
		}
	};
	/* 审批 */
	approve = () => {
		const { props, action } = this.comp;
		const { dispatch, psninfoApprove, form } = props;
		const { hrorgobj, approveStatus, selectedRowPk, approveComment, showMode } = psninfoApprove;
		let params = {
			pk_org: hrorgobj.refpk,
			checkstatus: approveStatus,
			pks: selectedRowPk.join(),
			checknote: approveComment
		};
		dispatch({
			type: 'psninfoApprove/sendApprove',
			payload: {
				postData: params
			}
		}).then((res) => {
			if (showMode === 'list-browse') {
				this.pubSub.publish('setTableData');
			}
			if (showMode === 'card-browse') {
				this.setDetailData(selectedRowPk[0]);
			}

			dispatch({
				type: 'psninfoApprove/update',
				payload: {
					approveModalShow: false,
					approveStatus: 0,
					approveComment: '',
					selectedRowPk: []
				}
			});
		});
	};
}
